<?php
include_once 'MY/extra_sql.php';
class IndexController extends Zend_Controller_Action{
	function init(){
		if($_SESSION['user']==""){
			echo "<script>window.location='./user/login';</script>";exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
		$this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->template = "index";
	}
    

public function lineAction(){
		$project = new Project();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		$filter=new Zend_Filter_StripTags();
		$project_type = $this->view->conf ->projectType;
		$type = array();
	    foreach($project_type as $s=>$l){
	    	$type[] = $l;
	    }
		$key_id=$filter->filter(($this->_request->getParam('key')));
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrProjectType[] = array(0,0,0,0);
		}
		//$arrYear = array_reverse($arrYear);
		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $project->getProjectByTime($row[0] ,$row[1]);
			foreach($arrProject as $k=>$v){
				$arrProjectType[$key][$arrProject[$k]['category']]++;
			}
		}
		$count = count($arrProjectType);
		for($i=0;$i<$count;$i++){
			foreach($arrProjectType[$i] as $key=>$row){
				$arrTypeNum[$key][] = $row;
			}
		}
		
		include './public/chart/php-ofc-library/open-flash-chart.php';

		$arr1 = $arrTypeNum[$key_id];
		
		$intMax = max($arr1);
		$y_num = ($intMax>=10)?ceil($intMax/10)*10:10;
		
		$data_1 = $arr1;
		
		$default_dot = new dot();
		$default_dot->size(5)->colour(rand_color());
		
		$line_dot = new line();
		$line_dot->set_default_dot_style($default_dot);
		$line_dot->set_width( 4 );
		$line_dot->set_colour(rand_color());
		$line_dot->set_values( $data_1 );
		$line_dot->set_key( $type[$key_id], 10 );
		
		$y = new y_axis();
		$y->set_range( 0, $y_num, $y_num/10 ); 
		
		$x = new x_axis();
		$x->set_labels_from_array( $arrYearShow );
		
		$chart = new open_flash_chart();
		$chart->set_title( new title( $type[$key_id].' student' ) );
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );

		$chart->add_element( $line_dot );
		
		echo $chart->toPrettyString();
	}
	
public function bars1Action(){
		//班级统计
		$class = new Classes();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		$project_type = $this->view->conf ->projectType;
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrClassType[] = array(0,0,0,0,0,0);
		}
		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrClass = array();
		foreach($arrTime as $key=>$row){
			$arrClass = $class->getClassByTime($row[0] ,$row[1]);
			foreach($arrClass as $k=>$v){
				$arrClassType[$key][$arrClass[$k]['category']]++;
			}
			//$arrClassType[$key]['year'] = $arrYear[$key];
		}
		
		$count = count($arrClassType);
		for($i=0;$i<$count;$i++){
			foreach($arrClassType[$i] as $key=>$row){
				$arrTypeNum[$key][] = $row;
			}
		}
		require_once('./public/chart/php-ofc-library/open-flash-chart.php');
		
		$title = new title( date("班级数量分布") );
		
		foreach ($project_type as $v=>$l){
			$arr[] = $arrTypeNum[$v ];
			$s[] = max($arrTypeNum[$v ]);
		}
		 
		$intMax = max($s);
		$y_num = ($intMax>=10)?ceil($intMax/10)*10:10;
		$ss = array();
				$y = new y_axis(); 
		$y->set_range( 0, $y_num, $y_num/10 );
		
		$x = new x_axis();
		$x->set_labels_from_array( $arrYearShow );
		
		$chart = new open_flash_chart();
		$chart->set_title( $title );
		
		foreach($project_type as $k=>$val){        
					$bar =  new bar_glass();
					$bar->set_values( $arr[$k] );
					$bar->set_colour(rand_color());
					$bar->set_on_show(new bar_on_show('pop-up',0.5,0.2));
					$bar->set_tooltip( $val.'：#val#' );			
					$chart->add_element( $bar );
					$ss[] = $bar;
		}
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );
		
		echo $chart->toPrettyString();
	}
	
	
public function barsAction(){
		$filter = new Zend_Filter_StripTags();
		$project = new Project();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		$project_type = $this->view->conf ->projectType;
		
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrProjectType[] = array(0,0,0,0);
		}
		//$arrYear = array_reverse($arrYear);
		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $project->getProjectByTime($row[0] ,$row[1]);
			foreach($arrProject as $k=>$v){
				$arrProjectType[$key][$arrProject[$k]['category']]++;
			}
		}
		$count = count($arrProjectType);
		for($i=0;$i<$count;$i++){
			foreach($arrProjectType[$i] as $key=>$row){
				$arrTypeNum[$key][] = $row;
			}
		}
		require_once('./public/chart/php-ofc-library/open-flash-chart.php');
		
		$title = new title( date("项目数量分布") );

		foreach ($project_type as $v=>$l){
			$arr[] = $arrTypeNum[$v ];
			$s[] = max($arrTypeNum[$v ]);
		}
		 
		$intMax = max($s);
		$y_num = ($intMax>=10)?ceil($intMax/10)*10:10;
		$ss = array();
				$y = new y_axis(); 
		$y->set_range( 0, $y_num, $y_num/10 );
		
		$x = new x_axis();
		$x->set_labels_from_array( $arrYearShow );
		
		$chart = new open_flash_chart();
		$chart->set_title( $title );
		
		foreach($project_type as $k=>$val){        
					$bar =  new bar_glass();
					$bar->set_values( $arr[$k] );
					$bar->set_colour(rand_color());
					$bar->set_on_show(new bar_on_show('pop-up',0.5,0.2));
					$bar->set_tooltip( $val.'：#val#' );			
					$chart->add_element( $bar );
					$ss[] = $bar;
		}
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );
		
		echo $chart->toPrettyString();
	}
	
	
	
	public function indexAction(){
		$project = new Project();
		$projects = $project->getAllProjects('','');
		$this->view->projecttotal = count($projects);
		$this->view->projecttop = $project->getTopProjects();
		$this->view->projects = $projects;
        $filter = new Zend_Filter_StripTags();
		$project = new Project();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrProjectType[] = array(0,0,0,0);
		}
		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $project->getProjectByTime($row[0] ,$row[1]);
			foreach($arrProject as $k=>$v){
				$arrProjectType[$key][$arrProject[$k]['category']]++;
			}
			$arrProjectType[$key]['year'] = $arrYear[$key];
		}
		$cc = array();
	    foreach ($arrProjectType as $k=>$v){
	    	$j=0;
	    	for($i=0;$i<count($v);$i++){
	    	  $cc[$i]+=$v[$i];	
	    	}	    	
	    }
	    $cc['year'] = '小结';
        $arrProjectType[] = $cc;
		$this->view->arrProjectType = $arrProjectType;
		
		
		$student = new Student();
		$students = $student->getAllStudents('', '', '');
		$this->view->studenttotal = count($students);
		$this->view->studenttop = $student->getTopStudents();
		$this->view->students = $students;

		$class = new Classes();
		$classes = $class->getAllClasses('','','','','','');
		$this->view->classtotal = count($classes);
		$this->view->classtop = $class->getTopClasses();
		$this->view->classes = $classes;
	    $arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrClassType[] = array(0,0,0,0);
		}
		
        
		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrClass = array();
		foreach($arrTime as $key=>$row){
			$arrClass = $class->getClassByTime($row[0] ,$row[1]);
			foreach($arrClass as $k=>$v){
				$arrClassType[$key][$arrClass[$k]['category']]++;
			}
			$arrClassType[$key]['year'] = $arrYear[$key];
		}
		$cc = array();
	    foreach ($arrClassType as $k=>$v){
	    	$j=0;
	    	for($i=0;$i<count($v);$i++){
	    	  $cc[$i]+=$v[$i];	
	    	}	    	
	    }
	    $cc['year'] = '小结';
        $arrClassType[] = $cc;
		$this->view->arrClassType = $arrClassType;
		
					
		$company = new Company();
		$companys = $company->getAllCompanys('','','','');
		$this->view->companys = $company->getAllcompanys(1,10,'','');
		$this->view->companytotal = count($companys);
		$this->view->companytop = $company->getTopCompanys();

		$course = new Course();
		$courses = $course->getAllCourses('','','','','');
		$this->view->coursetotal = count($courses);
		$this->view->coursetop = $course->getTopCourses();

		$teacher = new Teacher();
		$teachers = $teacher->getAllTeachers('','','');
		$this->view->teachertotal = count($teachers);
		$this->view->teachertop = $teacher->getTopTeachers();

		$tag = new Tag();
		$tags = $tag->getAllTags('','','','');
		$this->view->tagtotal = count($tags);
		$this->view->tagtop = $tag->getTopTags();
                
		echo $this->view->render('index/index.html');
    }
}

